from matplotlib import rcParams

#set plot attributes
fig_width = 4  # width in inches
fig_height = 3  # height in inches
fig_size =  [fig_width,fig_height]
params = {'backend': 'Agg',
          'axes.labelsize': 6,
          'axes.titlesize': 8,
          'text.fontsize': 8,
          'legend.fontsize': 6,
          'xtick.labelsize': 8,
          'ytick.labelsize': 8,
          'figure.figsize': fig_size,
          'savefig.dpi' : 600,
          'font.family': 'sans-serif'}
rcParams.update(params)


import numpy as np
import matplotlib.pyplot as plt

def sigmoid(x):
    return 1./(1+np.exp(-(x-5)))+1

np.random.seed(1234)
t = np.arange(0, 10., 0.15)
y = sigmoid(t) + 0.2*np.random.randn(len(t))
residuals = y - sigmoid(t)

t_fitted = np.linspace(0, 10, 100)

#adjust subplots position
fig = plt.figure()

ax1 = plt.axes((0.145, 0.15, 0.8, 0.775))
plt.plot(t, y, 'k.', label="data points")
plt.plot(t_fitted, sigmoid(t_fitted), 'k-',
         label="fitted function:\n$({1+e^{-t+5}})^{-1}+10$")

#set axis limits
ax1.set_xlim((0, 10.))
ax1.set_ylim((0.5, 2.5))

#hide right and top axes
ax1.spines['top'].set_visible(False)
ax1.spines['right'].set_visible(False)
ax1.spines['bottom'].set_position(('outward', 10))
ax1.spines['left'].set_position(('outward', 10))
ax1.yaxis.set_ticks_position('left')
ax1.xaxis.set_ticks_position('bottom')

#set labels
plt.xlabel(r'voltage ($\mu$V)')
plt.ylabel('luminescence')

#add legend
leg = plt.legend(loc="upper left")
leg.draw_frame(False)

#make inset
ax_inset = plt.axes((0.62, 0.22, 0.25, 0.25))
plt.hist(residuals, fc='w')
plt.xticks([-0.8, 0., 0.8], size=6)
plt.yticks([0, 20.], size=6)
plt.xlabel("residuals", size=6)
plt.ylabel("count", size=6)


#export to svg
plt.savefig('power_vs_synchrony.svg')
